request = require 'request'
db = require './connect'
async = require 'async'
fs = require 'fs'
path = require 'path'
request = require 'request'
# crawContent = (done)->
# 	sql = "select * from articles where state = 0 limit 1"
# 	db.runSql sql, (err, result)->
# 		async.forEachSeries result, (article, cb)->
# 			request.get article.contenturl, (err, res, body)->
# 				console.log arguments
# 				cb null

crawCover = (done)->
	sql = "select id,cover from articles order by datetime desc limit 2000"
	db.runSql sql, (err, result)->
		
		async.forEachLimit result, 10, (article, cb)->
			console.log article
			f = path.resolve(__dirname, '../public/covers/'+article.id+ '.jpg')
			unless fs.existsSync f
				console.log f
				ws = fs.createWriteStream path.resolve(__dirname, '../public/covers/'+article.id+'.jpg')
				request.get(article.cover, cb).pipe(ws)
			else
				cb null
		,(err)->
			console.log err if err


crawDetail = (done)->
	sql = "select id,title,contenturl from articles where state = 0"
	db.runSql sql, (err, result)->
		unless result?.length>0
			return done null
		async.forEachLimit result , 2, (article, cb)->
			console.log article.title
			request.get article.contenturl, (err, res, body)->
				console.log err if err
				db.runSql 'update ignore articles set `text` = ?, state = 1 where id = ? ', [body, article.id], cb
		,(err)->
			console.log err if err
			done null

run = ()->
	async.series [crawDetail,crawCover],(err)->
		console.log err

run()
		
crawCover()
